package zhh.tangbao.machine.factory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import zhh.tangbao.machine.pojo.PreConnectionVerificationMachine;
import zhh.tangbao.machine.socket.client.MachineSocketClient;
import zhh.tangbao.machine.socket.handler.MachineSocketDisconnectHandler;
import zhh.tangbao.machine.socket.handler.impl.DefaultMachineSocketDisconnectHandler;

import java.net.Socket;
import java.util.Date;
import java.util.Timer;

/**
 * <p>
 *     封装timer的创建步骤以及timerTask的执行步骤，简化业务代码
 * </p>
 * @author 粥灰灰
 * <p>2023/3/25</p>
 */

@Component
public class MachineSocketClientFactory {

    /** 任务间隔，以毫秒为单位 */
    @Value("${custom.socket.heart}")
    private long heart;

    @Autowired
    private MachineSocketDisconnectHandler handler;

    /**
     * 创建一个定时任务，但不启动任务
     */
    public MachineSocketClient createClient(PreConnectionVerificationMachine machine, Socket clientSocket) {
        return new MachineSocketClient(machine, clientSocket, new Timer(), handler);
    }

    /**
     * 使用工厂中的固定配置启动task
     */
    public void scheduleTask(MachineSocketClient client) {
        client.schedule(heart);
    }

}
